<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class Layout
{

    private $_CI;
    public $_langCode = '';
    private $_menu = array();

    function __construct()
    {
        $this->_CI = & get_instance();
        $this->_CI->load->library('Minifier');
        $this->_CI->load->library('Mycommon');
        $this->_langCode = $this->_CI->mycommon->getLangCode();
        $this->_CI->load->language('landing/landing', $this->_langCode);
        $this->_initMenu();
    }

    public function _loadHeader($headTitle = '')
    {
        $data = array();
        $this->_CI->config->load('config_info');
        $configInfo = $this->_CI->config->item('infoWebsite');
        $data['headTitle'] = ($headTitle != '') ? $headTitle : $configInfo['headTitle'];
        $data['metaTitle'] = $configInfo['metaTitle'];
        $data['metaKeyword'] = $configInfo['metaKeyword'];
        $data['metaDesc'] = $configInfo['metaDesc'];
        $this->addCssJs();
        $data['combinedCss'] = $this->_CI->minifier->GetCombined('css');
        $data['combinedJs'] = $this->_CI->minifier->GetCombined('js');
        $this->_CI->load->view('header_view', $data);
    }

    public function _loadMenuBar()
    {
        $data = array();
        $data['lang'] = $this->_CI->lang->line('landing_lang');
        $data['segment'] = $this->_CI->uri->rsegment(1);
        $data['menu'] = $this->_menu;
        $data['langCode'] = $this->_langCode;
        $this->_CI->load->view('menubar_view', $data);
    }

    private function _initMenu()
    {

        $this->_CI->load->library('Menu');
        $this->_CI->load->library('Blockcache');
        if ($menu = $this->_CI->blockcache->get('menubar')) {
            $this->_menu = $menu;
        } else {
            $this->_CI->load->model('option/option_model');
            $this->_CI->load->model('category/category_model');
            $langCode = $this->_CI->mycommon->fetchLangCode($this->_langCode);
            $lstMenubar = $this->_CI->option_model->getOptionByName('lstMenubar');
            $arrMenubar = explode(',', $lstMenubar['value']);
            $posParent = 0;
            foreach ($arrMenubar as $v) {
                if ($tmpInfo = $this->_CI->category_model->getCategoryInfo($v, $langCode)) {
                    $catInfo = $tmpInfo[0];
                    $path = (($this->_langCode == 'vietnam') ? 'chuyen-muc/' : 'category/') . $this->_CI->mycommon->convertToAlias($catInfo['name']) . '_' . $catInfo['id'];
                    if ($catInfo['href'] == '')
                        $url = site_url($path);
                    else
                        $url = $catInfo['href'];
                    $this->_CI->menu->addMenu($catInfo['id'], $catInfo['name'], $url, $posParent, $catInfo['parent_id']);
                    $posParent++;
                    if ($tmpChild = $this->_CI->category_model->getChildrenCategory($catInfo['id'], $langCode)) {
                        $posChild = 0;
                        foreach ($tmpChild as $v) {
                            $path = (($this->_langCode == 'vietnam') ? 'chuyen-muc/' : 'category/') . $this->_CI->mycommon->convertToAlias($v['name']) . '_' . $v['id'];
                            if ($v['href'] == '')
                                $url = site_url($path);
                            else
                                $url = $v['href'];
                            $this->_CI->menu->addSubmenu($v['parent_id'], $v['id'], $v['name'], $url, $posChild);
                            $posChild++;
                        }
                    }
                }
            }
            $this->_menu = $this->_CI->menu->renderMenu();
            $this->_CI->blockcache->save('menubar', $this->_menu, 3000);
        }
    }

    public function setMenuActive($menuid)
    {
        foreach ($this->_menu as $k => $v) {
            if ($v->id == $menuid) {
                if ($v->parent != 0)
                    $this->setMenuActive($v->parent);
                else
                    $this->_menu[$k]->active = true;
            }
        }
    }

    public function _loadColLeft()
    {
        $data = array();
        $data['lang'] = $this->_CI->lang->line('landing_lang');
        $this->_CI->load->view('colleft_view', $data);
    }

    public function _loadFooter()
    {
        $this->_CI->load->view('footer_view');
    }

    private function addCssJs()
    {
        $this->_CI->minifier->AddCss('base');
        $this->_CI->minifier->AddJs('jquery-1.8.2.min');
        $this->_CI->minifier->AddJs('tooltip');
    }

    public function view($viewFile, $data = array())
    {
        $headTitle = (isset($data['headTitle'])) ? $data['headTitle'] : '';
        $this->_loadHeader($headTitle);
        $this->_loadMenuBar();
        $this->_CI->load->view($viewFile, $data);
        $this->_loadColLeft();
        $this->_loadFooter();
    }

}

?>
